package com.example.wangda.smartparking;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.media.RemoteControlClient;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.navisdk.adapter.BNOuterTTSPlayerCallback;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.chinamobile.iot.onenet.mqtt.MqttCallBack;
import com.chinamobile.iot.onenet.mqtt.MqttClient;
import com.chinamobile.iot.onenet.mqtt.MqttConnectOptions;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttConnAck;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttMessage;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttPublish;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttSubAck;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttSubscribe;
import com.chinamobile.iot.onenet.mqtt.protocol.imp.Type;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;

import overlayutil.DrivingRouteOverlay;
import overlayutil.OverlayManager;
import overlayutil.TransitRouteOverlay;
import overlayutil.WalkingRouteOverlay;

import static android.content.ContentValues.TAG;
import static com.baidu.navisdk.adapter.PackageUtil.getSdcardDir;
import static com.chinamobile.iot.onenet.mqtt.protocol.imp.QoS.AT_MOST_ONCE;

public class ParkingAvtivity extends Activity implements BaiduMap.OnMapLoadedCallback ,
        OnGetRoutePlanResultListener {

    private TextView textView_mapDate;
    private Button btn_location;
    private Button btn_clear;
    private Button btn_navi;
    private Button btn_reserve;
    private TextView textView_space;
    private TextView textView_mapdate;
    private TextView textView_reserve;
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private boolean mapLoaded = false;
    private int mCurrentDirection = 0;
    /*纬度*/
    private double mCurrentLat = 0.0;
    /*经度*/
    private double mCurrentLon = 0.0;
    /*半径 精度*/
    private float mCurrentAccracy;
    /*位置信息*/
    private String mCurrentLocationInfo;
    private MyLocationData locData;
    LocationClient mLocClient;
    private SensorManager mSensorManager;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    BitmapDescriptor mCurrentMarker;
    BitmapDescriptor bitmap;
    OverlayOptions option;
    private Overlay myOverlay;
    private int myOverlay_st = 0;
    LatLng savePoint;
    LatLng currentPoint;
    boolean useDefaultIcon = false;
    boolean isFirstLoc = true; // 是否首次定位
    OverlayManager routeOverlay = null;
    DrivingRouteOverlay overlay;
    int parkingNum=0;
    int carportSta=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SDKInitializer.initialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking);
        textView_mapDate = (TextView) findViewById(R.id.textView_mapdate);
        btn_location = (Button) findViewById(R.id.btn_location);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_navi = (Button) findViewById(R.id.btn_find);
        btn_reserve = (Button)findViewById(R.id.btn_reserve);
        textView_reserve = (TextView)findViewById(R.id.textView_reserve);
        textView_space = (TextView)findViewById(R.id.textView_space);
        textView_mapdate = (TextView)findViewById(R.id.textView_mapdate);
        MqttInit(ParkingAvtivity.this);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//获取传感器管理服务
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        /*map初始化*/
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all");
        option.setScanSpan(1000);
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果
        mLocClient.setLocOption(option);
        mLocClient.start();
        /*跟踪模式*/
        {
            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.overlook(0);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        }
        /*默认图标*/
        {
            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, true, null));
        }
        /*预约车位*/
        btn_reserve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView_reserve.setText(""+ reservaParking());
                MqttPublishMessage(""+ reservaParking());
            }
        });
        /*定位车辆*/
        btn_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setUserMapCenter();
                if(myOverlay_st != 0)
                {
                    new AlertDialog.Builder(ParkingAvtivity.this).setTitle("提示")
                            .setMessage("您当前已经有一个定位信息，请先删除后再定位！")
                            .setPositiveButton("好的！", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            }).show();
                    return;
                }
                new AlertDialog.Builder(ParkingAvtivity.this).setTitle("提示")
                        .setMessage("您确定当前定位准确，并且保存当前位置么？" + "\n"
                                + "             " + mCurrentLocationInfo + "\n"
                                + "             " + "定位精度误差：" + (int) mCurrentAccracy + " 米")
                        .setPositiveButton("是的！", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                /*放置Mark点*/
                                setMarker();
                                /*保存位置数据*/
                                savePoint = new LatLng(mCurrentLat, mCurrentLon);
                                new AlertDialog.Builder(ParkingAvtivity.this)
                                        .setTitle("提示")
                                        .setMessage("已经为您保存当前位置，稍后点击查找车辆，帮您导航到当前位置")
                                        .setPositiveButton("确定", null).show();
                            }
                        }).setNegativeButton("再等等！", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).show();
            }
        });

        /*清除定位*/
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(ParkingAvtivity.this).setTitle("提示")
                        .setMessage("您确定已经不需要当前的定位信息了么？")
                        .setPositiveButton("是的！", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                removeMarker();
                                removeDriving();
                            }
                        }).setNegativeButton("再等等！", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).show();
            }
        });
        /*定位导航*/
        btn_navi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPoint = new LatLng(mCurrentLat, mCurrentLon);
                NaviWalkInit();
            }
        });

    }
    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            mCurrentLat = location.getLatitude();
            mCurrentLon = location.getLongitude();
            mCurrentAccracy = location.getRadius();
            mCurrentLocationInfo = location.getLocationDescribe();
            //textView_mapDate.setText("经度：" + mCurrentLat + "纬度：" + mCurrentLon + "精度："+ mCurrentAccracy + "\n" + mCurrentLocationInfo);
            textView_mapDate.setText(""+mCurrentLocationInfo);

            locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(mCurrentDirection).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    /**
     * 添加marker
     */
    private void setMarker() {
        Log.v("pcw","setMarker : lat : "+ mCurrentLat+" lon : " + mCurrentLon);
        //定义Maker坐标点
        LatLng point = new LatLng(mCurrentLat, mCurrentLon);
        //构建Marker图标
        bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.location_marker);
        //构建MarkerOption，用于在地图上添加Marker
        option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        myOverlay = mBaiduMap.addOverlay(option);
        myOverlay_st = 1;
    }

    /**
     * 删除Mark点
     */
    private void removeMarker(){
        if(myOverlay != null) {
            myOverlay.remove();
            myOverlay_st = 0;
        }
    }

    /*
    * 删除导航轨迹
    * */
    private void removeDriving(){
        if(overlay != null){
            overlay.removeFromMap();
        }
    }
    /**
     * 设置中心点
     */
    private void setUserMapCenter() {
        Log.v("pcw","setUserMapCenter : lat : "+ mCurrentLat+" lon : " + mCurrentLon);
        LatLng cenpt = new LatLng(mCurrentLat,mCurrentLon);
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(18)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
    }

    /*路线规划*/
    private void NaviWalkInit()
    {
        RoutePlanSearch mSearch = null;    // 搜索模块，也可去掉地图模块独立使用
        // 初始化搜索模块，注册事件监听
        if (savePoint != null && currentPoint != null) {
            PlanNode stNode = PlanNode.withLocation(currentPoint);
            PlanNode enNode = PlanNode.withLocation(savePoint);
            mSearch = RoutePlanSearch.newInstance();
            mSearch.setOnGetRoutePlanResultListener(this);
            mSearch.drivingSearch((new DrivingRoutePlanOption())
                    .from(stNode).to(enNode));
        }
    }

    /***************************MQTT SERVER***********************/
    public void MqttInit(Context context) {
        String apikey = "57khJ52JlHjPTydT6en=ru5=45Y=";
        String clientId = "23253479";
        String product_id = "113096";
        // 1、初始化SDK
        MqttClient.initialize(context, "183.230.40.39", 6002, clientId, product_id, apikey);
        // 2、设置接收响应的回调
        MqttClient.getInstance().setCallBack(callBack);
        // 3、设置连接属性(eg:心跳包。选填,可以不设置,调用connect()方法)
        MqttConnectOptions connectOptions = new MqttConnectOptions();
        connectOptions.setCleanSession(true);
        connectOptions.setKeepAlive(200);
        connectOptions.setWill(false);
        connectOptions.setWillQoS(AT_MOST_ONCE);
        connectOptions.setWillRetain(false);
        // 4、建立TCP连接
        MqttClient.getInstance().connect(connectOptions);
    }
    /*连接MQTT服务器*/
    public void UserConnect(Context context)
    {
        MqttClient.getInstance().disconnect();

        String apikey = "Yd9VgMbOlERW5qX7MtQnSGk5kfU=";
        String clientId = "19946427";
        String product_id = "99461";
        // 1、初始化SDK
        MqttClient.initialize(context, "183.230.40.39", 6002, clientId, product_id, apikey);
        // 2、设置接收响应的回调
        MqttClient.getInstance().setCallBack(callBack);
        MqttConnectOptions connectOptions = new MqttConnectOptions();
        connectOptions.setCleanSession(true);
        connectOptions.setKeepAlive(200);
        connectOptions.setWill(false);
        connectOptions.setWillQoS(AT_MOST_ONCE);
        connectOptions.setWillRetain(false);
        // 4、建立TCP连接
        MqttClient.getInstance().connect(connectOptions);
    }
    /*断开连接MQTT服务器*/
    public void UserUnconnect()
    {
        MqttClient.getInstance().disconnect();
    }
    /*创建回调函数（用来接收MQTT消息，继承CallBack接口）*/
    private MqttCallBack callBack = new MqttCallBack() {
        @Override
        public void messageArrived(MqttMessage msg) {
            //连接响应
            if (msg.getMqttHeader().getType() == Type.CONNACK) {
                MqttConnAck mqttConnAck = (MqttConnAck) msg;
                //透传消息 server->client
                MqttClient.getInstance().subscribe(new MqttSubscribe("android", AT_MOST_ONCE));
                Log.d(TAG, "MQTT Connect");
            } else if (msg.getMqttHeader().getType() == Type.PUBLISH) {
                MqttPublish mqttPublish = (MqttPublish) msg;
                String topic = mqttPublish.getTopicName();
                int msgId = mqttPublish.getMsgID();
                byte[] data = mqttPublish.getData();
                MqttRecvDateHandle(topic, msgId, data);
                Log.d(TAG, "MQTT PUBLISH");
                //订阅响应
            } else if (msg.getMqttHeader().getType() == Type.SUBACK) {
                MqttSubAck mqttSubAck = (MqttSubAck) msg;
                Log.d(TAG, "MQTT SUBACK");
                //心跳响应
            } else if (msg.getMqttHeader().getType() == Type.PINGRESP) {
                Log.d(TAG, "MQTT PINGRESP");
                //发布响应QOS1
            } else if (msg.getMqttHeader().getType() == Type.PUBACK) {
                //发布响应QOS2
            } else if (msg.getMqttHeader().getType() == Type.PUBCOMP) {

            }
        }
        //网络异常导致的断开
        @Override
        public void connectionLost(Exception e) {
            UserConnect(ParkingAvtivity.this);
            Log.d(TAG, "MQTT 网络异常导致的断开");
        }
        //连接断开
        @Override
        public void disconnect() {
            UserConnect(ParkingAvtivity.this);
            Log.d(TAG, "MQTT 连接断开");
        }
    };
    /*MQTT发布消息*/
    public void MqttPublishMessage(String str)
    {
        MqttPublish user_publish = new MqttPublish("parking",str.getBytes(),AT_MOST_ONCE);
        MqttClient.getInstance().sendMsg(user_publish);
    }

    /*查询剩余车位*/
    public int readRemainCarport()
    {
        return parkingNum;
    }
    /*查询车位状态*/
    public int readCarportSta()
    {
        return carportSta;
    }
    /*预约车位*/
    public int reservaParking()
    {
        for(int for_i=0; for_i<8;for_i++){
            if(((carportSta>>for_i)&0x01) == 0x01)
            {
                return for_i+1;
            }
        }
        return 0;
    }
    /*处理接收消息*/
    private void MqttRecvDateHandle(String topic, int msgId, byte[] data)
    {
        String strData = new String(data).trim();
        if(!topic.equals("android"))return;
        carportSta = Integer.parseInt(strData);
        Log.d(TAG, "int is" + carportSta);
        parkingNum = 0;
        for(int for_i=0; for_i<8;for_i++){
            if(((carportSta>>for_i)&0x01) == 0x01)
            {
                parkingNum++;
            }
            else
            {
                Log.d(TAG, "车位 " + for_i + "被占");
            }
        }
        textView_space.setText(""+ readRemainCarport());
        Log.d(TAG, "parkingNum is " + parkingNum);
    }

    @Override
    protected void onStop() {
        //取消注册传感器监听
        //mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        UserUnconnect();

    }

    @Override
    public void onMapLoaded() {
        mapLoaded = true;
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(ParkingAvtivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            //result.getSuggestAddrInfo()
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            routeOverlay = overlay;
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }

    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult result) {

        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(ParkingAvtivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            //result.getSuggestAddrInfo()
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            routeOverlay = overlay;
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(ParkingAvtivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            //result.getSuggestAddrInfo()
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            overlay = new MyDrivingRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            routeOverlay = overlay;
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult)
    {

    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult)
    {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult)
    {

    }


    //定制RouteOverly
    private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
            }
            return null;
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
            }
            return null;
        }
    }

    private class MyWalkingRouteOverlay extends WalkingRouteOverlay {

        public MyWalkingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
            }
            return null;
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
            }
            return null;
        }
    }

    private class MyTransitRouteOverlay extends TransitRouteOverlay {

        public MyTransitRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
            }
            return null;
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
            }
            return null;
        }
    }

    /**
     * 切换路线图标，刷新地图使其生效
     * 注意： 起终点图标使用中心对齐.
     */
    public void changeRouteIcon(View v) {
        if (routeOverlay == null) {
            return;
        }
        if (useDefaultIcon) {
            ((Button) v).setText("自定义起终点图标");
            Toast.makeText(this,
                    "将使用系统起终点图标",
                    Toast.LENGTH_SHORT).show();

        } else {
            ((Button) v).setText("系统起终点图标");
            Toast.makeText(this,
                    "将使用自定义起终点图标",
                    Toast.LENGTH_SHORT).show();

        }
        useDefaultIcon = !useDefaultIcon;
        routeOverlay.removeFromMap();
        routeOverlay.addToMap();
    }

}
